﻿package njust.struts2.action;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import njust.dao.GoodsDao;
import njust.dao.NeedsDao;
import njust.dao.ImageDao;
import njust.dao.UserDao;
import njust.domain.Goods;
import njust.domain.Image;
import njust.domain.Message;
import njust.domain.User;


//
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class GoodsAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Goods goods;
	private GoodsDao goodsDao;
	private NeedsDao needsDao;
	private String message;
	private UserDao userDao;
	private ImageDao imageDao;
	private List<File> imgfile;
	private String searchkeywords;
	

	private User user;

	private List<String> imgfileFileName;
	

	private List<String> imgfileContentType;
	
	private String style;

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}
	
	
	
	

	public String addGoods() throws ParseException, IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		User user2 = (User) session.getAttribute("user");
		// User user=userDao.getUserByName(userName);
		if (goods.getGoodname().equals("") || user2 == null) {
			message = "不能为空";
			return "addGoodError";
		}
		
		System.out.println(1231231);
		user2.setPhonenumber(user.getPhonenumber());
		user2.setQq(user.getQq());
		goods.setUser(user2);

		goods.setTime(new java.sql.Timestamp(Calendar.getInstance().getTime()
				.getTime()));
		goods.setState("正常");
		goodsDao.addGoods(goods);
		int goodsId=goods.getId();
		String realpath = ServletActionContext.getServletContext().getRealPath(
				"/upload/"+goodsId);
		List<Image> list=new ArrayList<Image>();
		for(int i=0;i<imgfile.size();i++){
			File savefile = new File(new File(realpath), imgfileFileName.get(i));
			if (!savefile.getParentFile().exists())
				savefile.getParentFile().mkdirs();
			FileUtils.copyFile(imgfile.get(i), savefile);
			Image image = new Image();
			image.setUrl("upload/"+goodsId +"/"+ imgfileFileName.get(i));
			if(i==0)
				goods.setUrl(image.getUrl());
			goods.getImage().add(image);
			image.setGoods(goods);
			list.add(image);
		}
		if (goodsDao.updateGoods(goods)) {
			for(int i=0;i<list.size();i++)
				imageDao.addImage(list.get(i));
			message = "添加成功";
			List<Message> messagelist = new ArrayList<Message>();
			messagelist.addAll(goods.getMessage());
			List<User> userlist = new ArrayList<User>();
			for (int i = 0; i < messagelist.size(); i++)
			{
				userlist.add(userDao.getUserById((messagelist.get(i).getUserid())));
			}
			ActionContext.getContext().put("userList", userlist);
			ActionContext.getContext().put("goods", goods);
			ActionContext.getContext().put("imgList", list);
			
			return "addGoodSuccess";
		} else {
			message = "添加失败";
			return "addGoodError";
		}
		
	}

	public String updateGoods() {
		if (goodsDao.updateGoods(goods)) {
			message = "更新成功";
			return "updateGoodok";
		} else {
			message = "更新失败";
			return "updateGooderror";
		}
	}

	public String deleteGoods(int id) {
		if (goodsDao.deleteGoods(id)) {
			message = "删除成功";
			return "deletegoodok";
		} else {
			message = "删除失败";
			return "deletegooderror";
		}
	}

	public String getAllGoods() {
		List<Goods> productList = new ArrayList();
		productList = goodsDao.getAllGoods();
		HttpServletRequest re = ServletActionContext.getRequest();
		re.setAttribute("goodsList", productList);
		re.setAttribute("jspnum", 1);
		return "getallOK";
	}
	
	public String getAllGoodsByTime() {
		List<Goods> productList = new ArrayList();
		productList = goodsDao.getAllGoods();
		HttpServletRequest re = ServletActionContext.getRequest();
		re.setAttribute("goodsList", productList);
		re.setAttribute("jspnum", 2);
		return "getAllGoodsByTime";
	}
	
	public String getGoodsSearch() {
		
		List<Goods> productList=new ArrayList<Goods>();
		productList = goodsDao.searchGoods(searchkeywords,style);
		HttpServletRequest re = ServletActionContext.getRequest();
		re.setAttribute("goodsList", productList);
		return "getGoodsSearch";
	}
	
	public String SearchGood() {
		
		List<Goods> productList=new ArrayList<Goods>();
		productList = goodsDao.searchGoods(searchkeywords);
		HttpServletRequest re = ServletActionContext.getRequest();
		re.setAttribute("goodsList", productList);
		return "getGoodsSearch";
	}

	public String getSearchkeywords() {
		return searchkeywords;
	}

	public void setSearchkeywords(String searchkeywords) {
		this.searchkeywords = searchkeywords;
	}

	public String getGoodById() {
		int id=Integer.parseInt(ServletActionContext.getRequest().getParameter("id"));
		List<Image> images = new ArrayList<Image>();
		goods = goodsDao.getGoodById(id);
		goods.setPageview(goods.getPageview()+1);
		goodsDao.updateGoods(goods);
		images.addAll(goods.getImage());
		List<Message> messagelist = new ArrayList<Message>();
		messagelist.addAll(goods.getMessage());
		List<User> userlist = new ArrayList<User>();
		for (int i = 0; i < messagelist.size(); i++)
		{
			userlist.add(userDao.getUserById((messagelist.get(i).getUserid())));
		}
		ActionContext.getContext().put("userList", userlist);
		ActionContext.getContext().put("goods", goods);
		ActionContext.getContext().put("imgList", images);
		if (goods != null)
			return "getGoodOK";
		else
			return "getGoodError";
	}
	
	
	public String getGoodByUserId() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		User us=(User)session.getAttribute("user");
		if(us==null) return "userCenterError";
		int id=us.getId();
		
		System.out.println(id);   
		List li= goodsDao.getGoodByUserId(id);
		
		ActionContext.getContext().put("goods", li);
		return "userCenter";
	}
	
	public String getGoodNeedByUserId() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		User us=(User)session.getAttribute("user");
		if(us==null) return "userCenterError";
		int id=us.getId();
	
		List goodsli= goodsDao.getGoodByUserId(id);
		List needsli= needsDao.getNeedByUserId(id);
		
		ActionContext.getContext().put("goods", goodsli);
		ActionContext.getContext().put("needs", needsli);
		return "userCenter";
	}


    
	public NeedsDao getNeedsDao() {
		return needsDao;
	}

	public void setNeedsDao(NeedsDao needsDao) {
		this.needsDao = needsDao;
	}

	public String getGoodByName(String name) {

		goods = goodsDao.getGoodByName(name);
		if (goods.getGoodname() != null)
			return "getGoodOK";
		else
			return "getGoodError";
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public GoodsDao getGoodsDao() {
		return goodsDao;
	}

	public void setGoodsDao(GoodsDao goodsDao) {
		this.goodsDao = goodsDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public ImageDao getImageDao() {
		return imageDao;
	}

	public void setImageDao(ImageDao imageDao) {
		this.imageDao = imageDao;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	

	public List<String> getImgfileFileName() {
		return imgfileFileName;
	}

	public void setImgfileFileName(List<String> imgfileFileName) {
		this.imgfileFileName = imgfileFileName;
	}

	public List<String> getImgfileContentType() {
		return imgfileContentType;
	}

	public void setImgfileContentType(List<String> imgfileContentType) {
		this.imgfileContentType = imgfileContentType;
	}

	public List<File> getImgfile() {
		return imgfile;
	}

	public void setImgfile(List<File> imgfile) {
		this.imgfile = imgfile;
	}

}